<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科室" prop="department">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入科室"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-table v-loading="loading" :data="doctorList" @selection-change="handleSelectionChange">-->
<!--      <el-table-column label="姓名" align="center" prop="name" />-->
<!--      <el-table-column label="图片" align="center" prop="image" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.image" :width="50" :height="50"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="职称" align="center" prop="titles" />-->
<!--      <el-table-column label="科室" align="center" prop="department" />-->
<!--      <el-table-column label="擅长治疗" align="center" prop="skill" />-->
<!--      <el-table-column label="简介" align="center" prop="info" />-->
<!--      <el-table-column label="入职时间" align="center" prop="entry" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.entry, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['doctor:doctor:edit']"-->
<!--          >修改</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->

    <el-row :gutter="10">
      <el-col :span="6" v-for="(doctor, index) in doctorList" :key="doctor.id" style="margin-bottom: 20px;">
        <el-card :body-style="{ padding: '20px', height: '350px', display: 'flex', flexDirection: 'column', justifyContent: 'space-between' }">
          <div style="display: flex; align-items: center; margin-bottom: 10px;">
            <!-- 医生头像 -->
            <image-preview :src="doctor.image" :width="100" :height="120" style="margin-right: 20px;"/>
            <div>
              <!-- 医生姓名 -->
              <h3>{{ doctor.name }}</h3>
              <!-- 医生职称 -->
              <p>{{ doctor.titles }}</p>
              <!-- 医生科室 -->
              <p>{{ doctor.department }}</p>
            </div>
          </div>
          <!-- 医生擅长治疗 -->
          <p><strong>擅长治疗：</strong>{{ doctor.skill }}</p>
          <!-- 操作按钮 -->
          <div style="margin-top: auto;">
            <el-button size="mini" type="text" @click="handleUpdate(doctor)">查看详细</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 展示中医名家信息的对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-row>
        <el-col :span="8">
          <!-- 医生头像 -->
          <image-preview :src="form.image" :width="100" :height="120"/>
        </el-col>
        <el-col :span="16">
          <div>
            <!-- 医生姓名 -->
            <p><strong>姓名：</strong>{{ form.name }}</p>
            <!-- 医生职称 -->
            <p><strong>职称：</strong>{{ form.titles }}</p>
            <!-- 医生科室 -->
            <p><strong>科室：</strong>{{ form.department }}</p>
          </div>
        </el-col>
      </el-row>
      <!-- 医生简介 -->
      <p><strong>简介：</strong>{{ form.info }}</p>
      <!-- 医生擅长治疗 -->
      <p><strong>擅长治疗：</strong>{{ form.skill }}</p>
      <!-- 入职时间 -->
      <p><strong>入职时间：</strong>{{ form.entry }}</p>
      <!-- 确定按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listDoctor, getDoctor, delDoctor, addDoctor, updateDoctor } from "@/api/doctor/doctor";

export default {
  name: "Doctor",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 中医名家表格数据
      doctorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        department: null,
        skill: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        department: [
          { required: true, message: "科室不能为空", trigger: "blur" }
        ],
        skill: [
          { required: true, message: "擅长治疗不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询中医名家列表 */
    getList() {
      this.loading = true;
      listDoctor(this.queryParams).then(response => {
        this.doctorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        image: null,
        titles: null,
        department: null,
        skill: null,
        info: null,
        entry: null,
        createTime: null,
        updateTime: null,
        createUser: null,
        updateUser: null,
        visit: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加中医名家";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDoctor(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "医师资料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDoctor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDoctor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
